package com.focusai.efairy.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;

import com.focusai.efairy.EFApplication;
import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DeviceItem;
import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.ui.HomeActivity;
import com.focusai.efairy.ui.activity.FunctionWizardActivity;
import com.focusai.efairy.ui.activity.dev.DevChattingActivity;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Locale;

import io.rong.imlib.RongIMClient;
import me.iwf.photopicker.PhotoPagerActivity;

/**
 * Created by luo_guangwen on 2017/8/8.
 */

public class ActivityUtils {
    /**
     * 启动App主页
     */
    public static void startMainHomeActivity(Context context) {
        Bundle extras = new Bundle();
        Intent intent = new Intent(context, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(extras);
        context.startActivity(intent);
    }

    /**
     * 启动聊天页
     */
    public static void startChattingActivity(Activity context, int requestCode, DeviceItem deviceItem) {
        Intent intent = new Intent(context, DevChattingActivity.class);
        intent.putExtra(DevChattingActivity.KEY_DEV_ITEM, deviceItem);
        if (requestCode != 0) {
            context.startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
        context.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);
    }


    public static void cleanAndStartLoginActivity(Activity activity) {
        CacheManager.loginOut();
//        RongIMClient.getInstance().disconnect();//断开与融云服务器的连接；
        RongIMClient.getInstance().logout();//断开与融云服务器的连接，并且不再接收 Push 消息。
        JiGuangPushUtil.loginOut();
        Intent intent = new Intent(activity, FunctionWizardActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    /**
     * 启动Activity
     */
    public static void startActivity(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    /**
     * 打开图片浏览器
     *
     * @param position 位置
     * @param urls     图片的路径或url
     */
    public static void openPhotoPagerActivity(Activity context,int reqeustCode, int position, ArrayList<String> urls, boolean isOpenDelete) {
        Intent intent = new Intent(context, PhotoPagerActivity.class);
        intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, position);
        intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, urls);

        if (isOpenDelete) {
            intent.putExtra(PhotoPagerActivity.EXTRA_SHOW_DELETE, true);
        } else {
            intent.putExtra(PhotoPagerActivity.EXTRA_SHOW_DELETE, false);
        }
        context.startActivityForResult(intent, reqeustCode);
    }

    public static boolean isActivityFinishing(Context context) {
        if (context == null) {
            return true;
        }

        if (context instanceof Activity) {
            return ((Activity) context).isFinishing();
        }

        return true;
    }

    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getVersionName() {
        PackageManager pm = EFApplication.getContext().getPackageManager();
        PackageInfo pi;
        try {
            pi = pm.getPackageInfo(EFApplication.getContext().getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "1.0.0";
    }

    // 隐藏软键盘
    public static void hideInputManager(Context ct) {
        try {
            InputMethodManager imm = (InputMethodManager) ct.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (!imm.isActive()) {
                return;
            }
            ((InputMethodManager) ct.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(((Activity) ct).getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            //Log.e("KDActivity", "hideInputManager Catch error,skip it!", e);
        }
    }
}
